#!/usr/bin/perl

require "./mailadmin-lib.pl";
&ReadParse();

my ($dname, @ulist, $password, $uquota, $err_msg, %uinfo);

unless (&qmail_dir_check())
{
    &redirect('maildir_edit.cgi');
}

$dname = $in{'dname'};
@ulist = split("\0", $in{'vuser'});

unless ($dname)
{
    $err_msg = $text{'msg_dname_empty'};
    goto ERROR_OUT;
}

unless ($in{'vuser'})
{
    $err_msg = $text{'msg_uname_empty'};
    goto ERROR_OUT;
}


&header($text{'index_title'}, "", undef, 0, 1);

if(@ulist == 1)
{

    &get_vuser_info($ulist[0], $dname, \%uinfo)
}

print "<form name=moduform action='save_modify_user.cgi' method=post>";

print "<table class=mode1 border=0 width=100%>\n";

print "<tr><td>$text{'dname'}</td><td><b>$in{'dname'}</b></td></tr>";

print "<tr><td valign=top width=15%>$text{'uname'}</td>\n";

print "<td><select name=unlist class=list size=7 value=\"\">\n";
for($i = 0; $i < @ulist; $i++)
{
    print "<option value=\"$ulist[$i]\">$ulist[$i]\n";
}
print "</select></td>\n";

print "</tr>";

print "<tr><td>$text{'password'}</td><td><input name=passwd class=text type=password value=\"$uinfo{'clear passwd'}\"></td></tr>\n";
print "<tr><td>$text{'confirm_pw'}</td><td><input name=con_passwd class=text type=password value=\"$uinfo{'clear passwd'}\"></td></tr>\n";
if (@ulist > 1) {
    printf "<tr><td>$text{'uquota'}</td><td><input name=uquota class=text size=8 value=\"\"><b>$text{'unit_MB'}</b></td></tr>\n";
} else {
    printf "<tr><td>$text{'uquota'}</td><td><input name=uquota class=text size=8 value=\"%.2f\"><b>$text{'unit_MB'}</b></td></tr>\n", &bytes2M($uinfo{'quota'});
    print "<tr><td>$text{'mbx_usage'}</td><td>$uinfo{'usage'}</td></tr>\n";
}


print "</table>";

print "<p><input name=dname type=hidden value=\"$in{'dname'}\">";

unless($in{'unamecache'}) {
    $in{'unamecache'} = join(':', @ulist);
}
print "<p><input name=unamecache type=hidden value=\"$in{'unamecache'}\">";

print "<p><input type=submit class=button1 value=\"$text{'btn_modify'}\" onclick=\"return modify_user();\">";


print "</form>";

print <<EOF;
<script>
function modify_user()
{
    if (document.moduform.unamecache.value == "")
    {
	alert("$text{'msg_uname_empty'}");
	return false;
    }

    if (document.moduform.passwd.value != document.moduform.con_passwd.value)
    {
	alert("$text{'msg_conpasswd_diff'}");
	return false;
    }
}
</script>
EOF

&footer("list_user.cgi?dname=$in{'dname'}", $text{"back_lsuser"});

exit 0;

ERROR_OUT:;

my (@headp, @footp);
@headp = ($text{'error'}, "");
@footp = ("list_user.cgi?dname=$in{'dname'}", $text{'back_lsuser'});
&mail_error(\@headp, \@footp, $text{'err_title_save_modify_user'}, $err_msg);
